package com.crm.gzcrm.web.controller;

import com.crm.gzcrm.model.CRUDResult;
import com.crm.gzcrm.model.CourseOrder;
import com.crm.gzcrm.model.PageResult;
import com.crm.gzcrm.service.ICourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("courseorder")
public class CourseOrderController {

    @Autowired
    ICourseOrderService orderService;
    @RequestMapping("list")
    public String list(){

        return "courseorder/list";
    }
    @RequestMapping("add")
    public String add(){
        return "courseorder/add";
    }

    @RequestMapping("edit")
    public String edit(Model model,String order_id){
        CourseOrder order = orderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        return "courseorder/edit";
    }

    @RequestMapping("detail")
    public String detail(Model model,String order_id){
        CourseOrder order = orderService.findByOrderId(order_id);
        model.addAttribute("order",order);
        return "courseorder/detail";
    }

    @RequestMapping("delete")
    @ResponseBody
    public CRUDResult delete(String order_id){
        CRUDResult result = new CRUDResult();
        orderService.deleteByOrderId(order_id);
        return result;
    }



    @RequestMapping("save")
    @ResponseBody
    public CRUDResult save(CourseOrder order){
        CRUDResult result = new CRUDResult();
        if (order.getOrder_id() == null || order.getOrder_id().length()==0){
            orderService.save(order);
        }else {
            orderService.update(order);
        }
        //System.out.println(order);

        return result;
    }

    /*
    *page 显示的多少页
    * limit显示的多少条
    * */
    @RequestMapping("listJson")
    @ResponseBody
    public PageResult<CourseOrder> listJson(CourseOrder condition,int page,int limit){
        System.out.println(condition);
        PageResult<CourseOrder> result = orderService.findPageResult(condition,page,limit);//limit就是pageSize

        return result;
    }

}
